package com.kye.express.business.common_tool_new.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.business.common_tool_new.CommonToolContract;
import com.kye.express.business.common_tool_new.viewmodel.CommonToolEditModel;
import com.kye.express.databinding.ItemCommonToolNewEditBinding;

import java.util.List;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 07/01/2018
 * @updated : 07/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class CommonToolEditItemViewBinder extends ItemViewBinder<CommonToolEditModel, CommonToolEditItemViewBinder.ViewHolder> {

    private CommonToolContract.CommonToolPresenter presenter;

    public CommonToolEditItemViewBinder(CommonToolContract.CommonToolPresenter presenter) {
        this.presenter = presenter;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_common_tool_new_edit, parent, false);
        return new ViewHolder(view, presenter);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull CommonToolEditModel item) {
        holder.bindData(item);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull CommonToolEditModel item, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, item, payloads);
    }

    @Override
    protected long getItemId(@NonNull CommonToolEditModel item) {
        return item.getId();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ItemCommonToolNewEditBinding binding;
        private CommonToolEditModel item;
        private CommonToolContract.CommonToolPresenter presenter;

        public ViewHolder(final View itemView, final CommonToolContract.CommonToolPresenter presenter) {
            super(itemView);
            this.presenter = presenter;
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.flContainer)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            Logger.d("item : " + item);
                            if (item.isSelected()) {
                                presenter.removeSelected(item);
                            } else {
                                presenter.addSelected(item);
                            }
                        }
                    });
        }

        public void bindData(CommonToolEditModel item) {
            this.item = item;
            int iconRes = R.drawable.icon_product;
            String[] names = itemView.getContext().getResources().getStringArray(R.array.common_tool_items_names);
            String title = names[item.getId() - 1];
            switch (item.getId()) {
                case 1:
                    iconRes = R.drawable.icon_product;
                    break;
                case 2:
                    iconRes = R.drawable.icon_customer;
                    break;
                case 3:
                    iconRes = R.drawable.icon_freight;
                    break;
                case 4:
                    iconRes = R.drawable.icon_time;
                    break;
                case 5:
                    iconRes = R.drawable.icon_online_pay;
                    break;
                case 6:
                    iconRes = R.drawable.icon_deposit_freight;
                    break;
                case 7:
                    iconRes = R.drawable.icon_general;
                    break;
                case 8:
                    iconRes = R.drawable.icon_more;
                    break;
                case 9:
                    iconRes = R.drawable.order_take_camera;
                    break;
                case 10:
                    iconRes = R.drawable.flight_prohibit;
                    break;
                case 11:
                    iconRes = R.drawable.jd_pre_order;
                    break;
                case 12:
                    iconRes = R.drawable.fan_support;
                    break;
                case 13:
                    iconRes = R.drawable.lucky_money_get;
                    break;
                default:
                    break;
            }
            item.setIconRes(iconRes);
            item.setTitle(title);
            item.setShowSelectedIcon(true);
            binding.setData(item);
        }
    }
}
